﻿<Page
    x:Name="PageRoot"
    x:Class="SensorsExample.SensorsExamplePage"
    DataContext="{Binding DefaultViewModel, RelativeSource={RelativeSource Self}}"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:SensorsExample"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:maps="using:Bing.Maps"
    xmlns:interactivity="using:Microsoft.Xaml.Interactivity"
    mc:Ignorable="d">
    <Page.Resources>
        <local:BasicGeopositionDisplayTextValueConverter x:Key="PositionToTextConverter"/>
    </Page.Resources>

    <!--
        This grid acts as a root panel for the page that defines two rows:
        * Row 0 contains the back button and page title
        * Row 1 contains the rest of the page layout
    -->
    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Grid.ChildrenTransitions>
            <TransitionCollection>
                <EntranceThemeTransition/>
            </TransitionCollection>
        </Grid.ChildrenTransitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="140"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!-- Back button and page title -->
        <Grid Grid.Row="0">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="120"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Button x:Name="BackButton" Margin="39,59,39,0" Command="{Binding NavigationHelper.GoBackCommand, ElementName=PageRoot}"
                        Style="{StaticResource NavigationBackButtonNormalStyle}"
                        VerticalAlignment="Top"
                        AutomationProperties.Name="Back"
                        AutomationProperties.AutomationId="BackButton"
                        AutomationProperties.ItemType="Navigation Button"/>
            <TextBlock x:Name="PageTitle" Text="{StaticResource AppName}" Style="{StaticResource HeaderTextBlockStyle}" Grid.Column="1" 
                        IsHitTestVisible="false" TextWrapping="NoWrap" VerticalAlignment="Bottom" Margin="0,0,30,40"/>
        </Grid>
        <Grid Grid.Row="1">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="Auto"/>
            </Grid.ColumnDefinitions>
            <Grid.Resources>
                <Style TargetType="TextBlock" BasedOn="{StaticResource BodyTextBlockStyle}">
                    <Setter Property="Margin" Value="5"/>
                </Style>
                <Style TargetType="TextBlock" BasedOn="{StaticResource BodyTextBlockStyle}" x:Key="BlockHeaderTextBlockStyle">
                    <Setter Property="Margin" Value="3,0,5,10"/>
                    <Setter Property="FontWeight" Value="Bold"></Setter>
                </Style>
                <Style TargetType="TextBox">
                    <Setter Property="Margin" Value="3,-2,3,5"/>
                </Style>
                <Style TargetType="Border">
                    <Setter Property="BorderThickness" Value="{StaticResource FlyoutBorderThemeThickness}"/>
                    <Setter Property="BorderBrush" Value="{StaticResource FlyoutBorderThemeBrush}"/>
                    <Setter Property="Padding" Value="5"/>
                    <Setter Property="Margin" Value="5"/>
                </Style>
            </Grid.Resources>
            <Grid Grid.Column="0" Width="320" DataContext="{Binding SensorSettings}" d:DataContext="{d:DesignInstance Type=local:SensorSettings, IsDesignTimeCreatable=true}">
                <Grid.RowDefinitions>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="Auto"/>
                </Grid.RowDefinitions>
                <StackPanel Grid.Row="0">
                    <Border>
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="10"/>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="10"/>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="Auto"/>
                            </Grid.RowDefinitions>
                            <TextBlock Grid.Row="0" Text="Location" Style="{StaticResource BlockHeaderTextBlockStyle}"/>
                            <TextBox Grid.Row="1" IsReadOnly="true" Text="{Binding LatestLocationReadingText}"/>
                            <CheckBox Grid.Row="2" Content="Request High Accuracy" IsChecked="{Binding IsLocationRequestingHighAccuracy, Mode=TwoWay}"/>
                            <Button Grid.Row="4" Content="Show Current Device Location" IsEnabled="{Binding IsLocationAvailable}" HorizontalAlignment="Stretch" Click="HandleShowCurentLocationRequest">
                                <Button.Flyout>
                                    <Flyout Placement="Right">
                                        <Grid Height="300" Width="250">
                                            <Grid.RowDefinitions>
                                                <RowDefinition Height="Auto"/>
                                                <RowDefinition Height="*"/>
                                            </Grid.RowDefinitions>
                                            <TextBlock Grid.Row="0" Text="Geocoordinate Details" Style="{StaticResource BlockHeaderTextBlockStyle}" HorizontalAlignment="Center"/>
                                            <ItemsControl Grid.Row="1" x:Name="DumpDataList"/>
                                        </Grid>
                                    </Flyout>
                                </Button.Flyout>
                            </Button>
                            <Button Grid.Row="5" Content="Center Map on Device Location" IsEnabled="{Binding IsLocationAvailable}" HorizontalAlignment="Stretch" Click="HandleCenterOnLocationRequest"/>
                            <Button Grid.Row="7" Content="List Fences"  IsEnabled="{Binding IsLocationAvailable}" HorizontalAlignment="Stretch" Click="HandleFenceListingRequest">
                                <FlyoutBase.AttachedFlyout>
                                    <Flyout Placement="Right">
                                        <local:ListGeofencesPanel/>
                                    </Flyout>
                                </FlyoutBase.AttachedFlyout>
                            </Button>
                            <Button Grid.Row="8" Content="Add Fence at Map Center"  IsEnabled="{Binding IsLocationAvailable}" HorizontalAlignment="Stretch" Click="HandleFenceLocationRequest">
                                <FlyoutBase.AttachedFlyout>
                                    <Flyout Placement="Right">
                                        <local:AddGeofencePanel/>
                                    </Flyout>
                                </FlyoutBase.AttachedFlyout>
                            </Button>
                        </Grid>
                    </Border>

                    <Border>
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="Auto"/>
                            </Grid.RowDefinitions>
                            <TextBlock Grid.Row="0" Text="Compass" Style="{StaticResource BlockHeaderTextBlockStyle}"/>
                            <TextBox Grid.Row="1" IsReadOnly="true" Text="{Binding LatestCompassReadingText}"/>
                            <Button Grid.Row="2" Content="Get Current" IsEnabled="{Binding IsCompassAvailable}" Click="HandleCompassRequest"/>
                            <CheckBox Grid.Row="3" Content="Follow" IsEnabled="{Binding IsCompassAvailable}" IsChecked="{Binding IsFollowingCompass, Mode=TwoWay}" />
                        </Grid>
                    </Border>

                    <Border>
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="Auto"/>
                            </Grid.RowDefinitions>
                            <TextBlock Grid.Row="0" Text="Inclinometer" Style="{StaticResource BlockHeaderTextBlockStyle}"/>
                            <TextBox Grid.Row="1" IsReadOnly="true" Text="{Binding InclinometerReadingText}"/>
                            <Button Grid.Row="2" Content="Get Current" IsEnabled="{Binding IsInclinometerAvailable}" Click="HandleInclinometerRequest" VerticalAlignment="Top"/>
                            <CheckBox Grid.Row="3" Content="Follow" IsEnabled="{Binding IsInclinometerAvailable}" IsChecked="{Binding FollowInclinometer, Mode=TwoWay}"/>
                        </Grid>
                    </Border>
                </StackPanel>
            </Grid>
            <Grid Grid.Column="1">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*"/>
                        <RowDefinition Height="Auto"/>
                    </Grid.RowDefinitions>
                    <maps:Map Grid.Row="0" Credentials="{StaticResource MapKey}" x:Name="ExampleMap">
                        <interactivity:Interaction.Behaviors>
                            <local:MapExtensions Position="{Binding Position, Mode=TwoWay}" Heading="{Binding Heading, Mode=TwoWay}" ZoomLevel="{Binding ZoomLevel, Mode=TwoWay}"/>
                        </interactivity:Interaction.Behaviors>
                    </maps:Map>
                    <Grid Grid.Row="1">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="2*"/>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>
                        <TextBlock Grid.Column="0" Text="Center: "/>
                        <TextBox Grid.Column="1" Text="{Binding Position, Converter={StaticResource PositionToTextConverter}}" IsReadOnly="True" Margin="5"/>
                        <TextBlock Grid.Column="2" Text="Zoom Level: "/>
                        <TextBox Grid.Column="3" Text="{Binding ZoomLevel}" IsReadOnly="True" Margin="5"/>
                    </Grid>
                </Grid>
            </Grid>
            <Grid Grid.Column="2" Width="320">
                <StackPanel DataContext="{Binding SensorSettings}" d:DataContext="{d:DesignInstance Type=local:SensorSettings, IsDesignTimeCreatable=true}">
                    <Border>
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="Auto"/>
                            </Grid.RowDefinitions>
                            <TextBlock Grid.Row="0" Text="Simple Orientation" Style="{StaticResource BlockHeaderTextBlockStyle}"/>
                            <TextBox Grid.Row="1" IsReadOnly="true" Text="{Binding LatestSimpleOrientationReading}"/>
                            <Button Grid.Row="2" Content="Get Current" IsEnabled="{Binding IsSimpleOrientationAvailable}" Click="HandleSimpleOrientationRequest"/>
                        </Grid>
                    </Border>

                    <Border>
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="Auto"/>
                            </Grid.RowDefinitions>
                            <TextBlock Grid.Row="0" Text="Accelerometer" Style="{StaticResource BlockHeaderTextBlockStyle}"/>
                            <TextBox Grid.Row="1" IsReadOnly="true" Text="{Binding LatestAccelerometerReadingText}"/>
                            <StackPanel  Grid.Row="2" Orientation ="Horizontal">
                                <Button Content="Get Current" IsEnabled="{Binding IsAccelerometerAvailable}" Click="HandleAccelerometerRequest"/>
                                <TextBlock Text="{Binding LatestAccelerometerShakeTimeText}"/>
                            </StackPanel>
                        </Grid>
                    </Border>

                    <Border>
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="Auto"/>
                            </Grid.RowDefinitions>
                            <TextBlock Grid.Row="0" Text="Gyrometer" Style="{StaticResource BlockHeaderTextBlockStyle}"/>
                            <TextBox Grid.Row="1" IsReadOnly="true" Text="{Binding LatestGyrometerReadingText}"/>
                            <Button Grid.Row="2" Content="Get Current" IsEnabled="{Binding IsGyrometerAvailable}" Click="HandleGyrometerRequest"/>
                        </Grid>
                    </Border>
                    
                    <Border>
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="Auto"/>
                            </Grid.RowDefinitions>
                            <TextBlock Grid.Row="0" Text="Orientation Sensor" Style="{StaticResource BlockHeaderTextBlockStyle}"/>
                            <TextBox Grid.Row="1" IsReadOnly="true" Text="{Binding LatestOrientationSensorReadingText}"/>
                            <Button Grid.Row="2" Content="Get Current" IsEnabled="{Binding IsOrientationSensorAvailable}" Click="HandleOrientationSensorRequest"/>
                        </Grid>
                    </Border>

                    <Border>
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="Auto"/>
                            </Grid.RowDefinitions>
                            <TextBlock Grid.Row="0" Text="Light Sensor" Style="{StaticResource BlockHeaderTextBlockStyle}"/>
                            <TextBox Grid.Row="1" IsReadOnly="true" Text="{Binding LatestLightSensorReadingText}"/>
                            <Button Grid.Row="2" Content="Get Current" IsEnabled="{Binding IsLightSensorAvailable}" Click="HandleLightSensorRequest"/>
                        </Grid>
                    </Border>

                </StackPanel>
            </Grid>
        </Grid>
    </Grid>
</Page>
